//comment.js
const util = require('../../utils/util.js');
const urls = require('../../utils/url.js');
const win = util.getSystemHW();
var componentObj = {
  properties:{
    param:{
      type: Object,
      value: null,
      observer: function(newVal, oldVal){}
    },
    viewHeight:{
      type:Number,
      value: win.windowHeight*0.6,
      observer: function(newVal, oldVal){}
    }
  },
  data: {
    animationData: {},
    modelAnimationData: {},
    viewWidth:win.windowWidth,
    modelHeight:win.windowHeight,
    list:[],
    hotList:[],
    hasMore:false,
    page:1,
    loaded:false,
    toView:'',
    modalFlag:false,
    text:'',
    isFocus:false,
    placeholder:'说点什么呢~~',
  },
  isEnd:false,
  //事件处理函数
  ready: function () {
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'linear',
      transformOrigin: "0% 0%",
    });
    var modelAnimation = wx.createAnimation({
      duration: 800,
      timingFunction: 'linear',
      transformOrigin: "100% 100%",
    });
    this.modelAnimation = modelAnimation;
    this.animation = animation;
    var thisObj = this;
    var curDistance = this.properties.param.dirction==='right'?-thisObj.data.viewWidth:thisObj.data.viewWidth;
    var aniObj = this.properties.param.dirction==='right'?{transformOrigin:"0% 0%"}:{transformOrigin:"100% 100%"};
    this.userObj = util.getStorageInfo('userInfo');
    this.animation.translateX(curDistance).backgroundColor("#fff").step(aniObj);
    this.setData({animationData:this.animation.export(),direction:this.properties.param.dirction},()=>{
      var curUrl = urls.comment+"&novel_id="+thisObj.properties.param.nId+"&mulu_id="+thisObj.properties.param.mId+"&content_id="+thisObj.properties.param.cId+"&page="+thisObj.data.page+"&pagesize=10";
      thisObj.getData(curUrl,(res)=>{
        var hotList = res.hot_reply;
        var list = res.list;
        thisObj.setData({loaded:true,hasMore:res.has_more>0?true:false,hotList:hotList,list:list})
      },true)
    })
  },

  methods:{
    __commentEvent: function(e){
      var newText = e.detail.value;
      this.setData({text:newText});
    },
    __sendComment: function(e){
      var paramObj = {};
      if(this.data.text&&this.data.text!==''&&util.stringTrim(this.data.text)){
        paramObj.novel_id = this.properties.param.nId;
        paramObj.mulu_id = this.properties.param.mId;
        paramObj.content_id = this.properties.param.cId;
        paramObj.content = this.data.text;
        paramObj.auth_key = this.userObj.auth_key;
        if(e.currentTarget.dataset.repalyId&&e.currentTarget.dataset.repalyId!==''){
          paramObj.reply_id= e.currentTarget.dataset.repalyId;
          paramObj.talk_user_id = this.userObj.uid;
        };
        this.__postDataCallBack(urls.publish,paramObj);
      }else{
        util.showTips("评论要有内容哦，不能全为空格！");
      }
    },
    __postDataCallBack: function(urlStr,paramObj,callback){
      var thisObj = this;
      this.postData(urlStr,paramObj,(res)=>{
        var toViewStr = "view_list_"+res.reply_id;
        var curUrl = urls.comment+"&novel_id="+paramObj.novel_id+"&mulu_id="+paramObj.mulu_id+"&content_id="+paramObj.content_id+"&page=1&pagesize=10";
        this.getData(curUrl,(res)=>{
          var hotList = res.hot_reply;
          var list = res.list;
          thisObj.setData({
            loaded:true,
            hasMore:res.has_more>0?true:false,
            hotList:hotList,
            list:list,
            page:1,
            toView:toViewStr,
            modalFlag:false,
            text:'',
            isFocus:false,
            placeholder:'说点什么呢~~'
          },()=>{
            if(callback){
              callback();
            }
          });
        },true)
      },false)
    },
    __confrimComment: function(e){
      this.__sendComment(e);
    },
    __praiseEvent: function(e){
      var paramObj = {};
      var thisObj = this;
      var curList = e.currentTarget.dataset.listType==='list'?this.data.list:this.data.hotList;
      paramObj.novel_id = this.properties.param.nId;
      paramObj.reply_id= e.currentTarget.dataset.replayId;
      paramObj.mulu_id= this.properties.param.mId;
      paramObj.auth_key= this.userObj.auth_key;
      this.postData(urls.praise,paramObj,(res)=>{
        curList[e.currentTarget.dataset.commentIndex]["like_num"] = (Number(curList[e.currentTarget.dataset.commentIndex]["like_num"])+1)+"";
        if(e.currentTarget.dataset.listType==='list'){
          thisObj.setData({
            list:curList
          })
        }else{
          thisObj.setData({
            hotList:curList
          })
        }
      },false);
    },
    __closeEvent: function(e){
      var thisObj = this;
      thisObj.modelAnimation.translateY(thisObj.data.modelHeight).step();
      thisObj.setData({
          modelAnimationData: thisObj.modelAnimation.export()
      },()=>{
        thisObj.setData({modalFlag: false,isFocus:false})
      })
    },
    __replayComment: function(e){
      var replayObj = {};
      var thisObj = this;
      var authName=e.currentTarget.dataset.userName;
      var content=e.currentTarget.dataset.commentText;
      replayObj.novel_id = this.properties.param.nId;
      replayObj.mulu_id = this.properties.param.mId;
      replayObj.content_id = this.properties.param.cId;
      replayObj.reply_id= e.currentTarget.dataset.replayId;
      replayObj.listType= e.currentTarget.dataset.listType;
      thisObj.replayObj = replayObj;
      this.setData({modalFlag:true,
        placeholder:'回复 '+authName+":"+content},()=>{
          thisObj.modelAnimation.translateY(-thisObj.data.modelHeight).step();
          thisObj.setData({
              modelAnimationData: thisObj.modelAnimation.export()
          })
        });

    },
    __callbackReply: function(e){
      var paramObj = {};
      var thisObj = this;
      if(this.data.text&&this.data.text!==''&&util.stringTrim(this.data.text)){
        paramObj.novel_id = this.replayObj.novel_id;
        paramObj.mulu_id = this.replayObj.mulu_id;
        paramObj.content_id = this.replayObj.content_id;
        paramObj.content = this.data.text;
        paramObj.auth_key = this.userObj.auth_key;
        paramObj.reply_id= this.replayObj.reply_id;
        paramObj.talk_user_id = this.userObj.uid;
        this.__postDataCallBack(urls.publish,paramObj,()=>{
          thisObj.__closeEvent();
        });
      }else{
        util.showTips("评论要有内容哦，不能全为空格！");
      }
    },
    __getInputFoces: function(e){
      this.setData({isFocus:true})
    },
    __modelSendComment: function(e){
      this.__callbackReply(e);
    },
    __closeCommentEvent: function(e){
      this.triggerEvent("closecomment",{});
    },
    getData: function(url,callback,isCustomLoading){
      if(util.isVailValue(url)){
        util.get(url,callback,isCustomLoading);
      }
    },
    postData: function(url,param,callback,isCustomLoading){
      if(util.isVailValue(url)){
        util.post(url,param,callback,isCustomLoading);
      }
    },
    commentLower: function(e){
      var thisObj = this;
      if(!thisObj.isEnd){
        thisObj.isEnd = true;
        var curPage = this.data.page+1;
        var curUrl = urls.comment+"&novel_id="+thisObj.properties.param.nId+"&mulu_id="+thisObj.properties.param.mId+"&content_id="+thisObj.properties.param.cId+"&page="+curPage+"&pagesize=10";
        thisObj.getData(curUrl,(res)=>{
          var newList = thisObj.data.list;
          Array.prototype.push.apply(newList,res.list);
          thisObj.setData({loaded:true,hasMore:res.has_more>0?true:false,list:newList,page:curPage},()=>{
            thisObj.isEnd = false;
          });
        },true)
      }else{

      }
    },
    getDomSize: function(id,callback,obj){
      util.getSeletorLoction(id,obj&&obj!==undefined&&obj!==null?obj:this,(res)=>{
        if(callback){
          callback(res);
        }
      })
    },
  }
};
Component(componentObj);
